<script setup lang="ts">
import { usePrintStore } from '@/stores'
import type { FormInstance, FormRules } from 'element-plus'
import { printMap } from '@/utils/mapPrint'
import { DY_MAP_VisibleScales } from '@/constants'

const printtingStore = usePrintStore()
const emit = defineEmits(['update:modelValue'])
const outOptionsForm = reactive<OutOptions>({
  format: 'PDF',
  DPI: '96',
  mapScale: '',
  mapCenter: '',
  mapZoom: 0,
  mapBounds: { south: 0, west: 0, north: 0, east: 0 }
})
const outOptionsFormRef = ref<FormInstance>()
const rules = reactive<FormRules<OutOptions>>({
  format: [{ required: true, message: '请选择输出格式', trigger: 'blur' }],
  DPI: [{ required: true, message: '请填写打印DPI', trigger: 'blur' }],
  mapScale: [{ required: true, message: '请填写地图比例尺', trigger: 'blur' }],
  mapCenter: [{ required: true, message: '请填写地图中心点', trigger: 'blur' }]
})

function toOtherForm(type: any) {
  emit('update:modelValue', type)
}

async function submitOutOptionsForm(formEl: FormInstance | undefined) {
  if (!formEl) return
  await formEl.validate(async (valid) => {
    if (!valid) return
    const bounds = map.getBounds()
    const south = bounds.getSouth()
    const west = bounds.getWest()
    const north = bounds.getNorth()
    const east = bounds.getEast()
    outOptionsForm.mapZoom = Math.round(map.getZoom())
    outOptionsForm.mapBounds = { south, west, north, east }
    try {
      printtingStore.submitOutOptionsForm(outOptionsForm)
      printtingStore.setFalse()
      await printMap()
    } catch (error) {
      throw new Error(error instanceof Error ? error.message : '地图打印失败')
    }
  })
}

onBeforeMount(() => {
  const zoom = map.getZoom()
  const { lng, lat } = map.getCenter()
  outOptionsForm.mapScale = DY_MAP_VisibleScales[Math.round(zoom)].toString()
  outOptionsForm.mapCenter = `${lng},${lat}`
})

watch(
  () => printtingStore.outOptions,
  (newVal) => Object.assign(outOptionsForm, newVal)
)
</script>

<template>
  <div>
    <el-form ref="outOptionsFormRef" :rules="rules" :model="outOptionsForm" label-position="right">
      <el-form-item label="格式：" prop="format" label-width="90px">
        <el-select v-model="outOptionsForm.format" placeholder="PDF" style="width: 300px">
          <el-option label="PNG" value="PNG" />
          <el-option label="PDF" value="PDF" />
        </el-select>
      </el-form-item>
      <el-form-item label="DPI：" prop="DPI" label-width="90px">
        <el-input v-model="outOptionsForm.DPI" autocomplete="off" style="width: 300px" />
      </el-form-item>
      <el-form-item label="比例尺：" prop="mapScale" label-width="90px">
        <el-select v-model="outOptionsForm.mapScale" placeholder="比例尺选择" style="width: 300px">
          <el-option :label="item" :value="item" :key="item" v-for="(item, index) in DY_MAP_VisibleScales">
            <span style="float: left">{{ item }}</span>
            <span style="float: right; color: var(--el-text-color-secondary); font-size: 13px">第 {{ index }} 级比例尺</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="中心点：" prop="mapCenter" label-width="90px">
        <el-input v-model="outOptionsForm.mapCenter" autocomplete="off" style="width: 300px" />
      </el-form-item>
    </el-form>
    <div class="btn-group">
      <el-button type="warning" @click="toOtherForm('scaleInfo')" style="margin-right: 50px">上一步</el-button>
      <el-button type="primary" @click="submitOutOptionsForm(outOptionsFormRef)">打印</el-button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.btn-group {
  margin-top: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
